package sfeir.ergosum;

import sfeir.ergosum.service.BackUpService;
import sfeir.ergosum.tools.ImportContacts;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;


public class PreferencesActivity extends PreferenceActivity {
    public static void startPreferenceAction(Context context) {
        context.startActivity(new Intent(
                context,
                PreferencesActivity.class));
    }
    
    public static void startPreferenceActionAuthentification(Context context) {
        Intent intent = new Intent(context, PreferencesActivity.class);
        intent.putExtra("authentification", true);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		findPreference("preference_backup").setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                BackUpService.actionBackup(PreferencesActivity.this);
                return true;
            }
		    
		});
		
		findPreference("preference_restore").setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                RestoreBackUpActivity.actionRestoreBackup(PreferencesActivity.this);
                return true;
            }
            
        });
		
	      findPreference("preference_import").setOnPreferenceClickListener(new OnPreferenceClickListener() {

	            @Override
	            public boolean onPreferenceClick(Preference preference) {
	                new ImportContacts().importAllContacts(PreferencesActivity.this);
	                return true;
	            }
	            
	        });
		
	}
	
	@Override
	protected void onResume() {
	    super.onResume();
	    Intent intent = getIntent();
	    if (intent.hasExtra("authentification") && intent.getBooleanExtra("authentification", false)) {
	        PreferencesLoginPass dia = (PreferencesLoginPass) findPreference("preference_loginpassKey");
	        dia.openDialog();
	    }
	}
}
